package com.aaa.service.Impl;

import com.aaa.mapper.ClazzMapper;
import com.aaa.pojos.Clazz;
import com.aaa.pojos.PackBean;
import com.aaa.pojos.Result;
import com.aaa.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ClazzServiceImpl implements ClazzService {
    @Resource
    private ClazzMapper clazzMapper;
    @Override
    public PackBean<Clazz> queryName(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
//        List<Clazz> c = clazzMapper.queryName(name,begin,end,page,pageSize);
//        log.info("listca"+c);
        Page<Clazz> clazz= (Page<Clazz>)clazzMapper.queryName(name,begin,end,page,pageSize);
        log.info(clazz+"clazz");
        for (Clazz clazz1 : clazz) {
            if (clazz1.getBeginDate().isBefore(LocalDate.now())&&clazz1.getEndDate().isAfter(LocalDate.now())){
                clazz1.setStatus("已开班");
            } else if (clazz1.getEndDate().isBefore(LocalDate.now())) {
                clazz1.setStatus("已结课");
            }else {
                clazz1.setStatus("未开班");
            }
        }
        return new PackBean<>((int)clazz.getTotal(),clazz.getResult());
    }

    @Override
    public void insertClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insertClazz(clazz);
    }

    @Override
    public Clazz queryId(Integer id) {
        Clazz clazz = clazzMapper.queryId(id);
        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }

    @Override
    public Boolean deleteClazz(Integer id) {
        Integer stu =null;
      stu= clazzMapper.student(id);
      log.info("stu值"+stu);
     if (stu == null){
         clazzMapper.deleteClazz(id);
         return true;
     }else{
         return false;
     }
    }

    @Override
    public List<Clazz> queryAll() {
        List<Clazz> clazz =clazzMapper.queryAll();
        return clazz;
    }


}
